package com.llh.controller.excel;

import com.llh.bean.StockBase;
import com.llh.bean.StockExport;
import com.llh.mapper.StockBaseMapper;
import com.llh.mapper.StockExportMapper;
import com.llh.util.PoiUtil;
import com.llh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 读取票基础信息的表
 */
public class ReadBaseExcel {
    /**
     * @param filePath 文件路径
     * @param sheetNum 表单索引
     * @param fistRow  第一列(列号非索引)
     * @param lastRow  最后一列(列号非索引)
     * @throws Exception
     */
    public void ReadBaseStock(String filePath, int sheetNum, int fistRow, int lastRow) throws Exception {

        File file = new File(filePath);//"D:\\文件\\限售股解禁公司明细.xls"
        String[] clNames = new String[]{
                "sCode", "sName", "openDay", "openNum", "cirNum",
                "cirValue", "openType", "totalNum", "totalRatio", "aftCirNum",
                "aftRatio", "befCirNum", "befRatio", "openCount", "annouDate"
        };
//        private String sSatus;
        Map<String, Map<String, Object>> mapMap = PoiUtil.readSheet(file, sheetNum, fistRow, lastRow, clNames);
        List<StockBase> stockBases = new LinkedList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = fistRow - 1; i < lastRow; i++) {
            try {
                Map<String, Object> map = mapMap.get(String.valueOf(i));
                StockBase stockBase = new StockBase();
                //"sCode","sName","openDay","openNum","cirNum",
                stockBase.setS_code((String) map.get(clNames[0]));
                stockBase.setS_name((String) map.get(clNames[1]));
                stockBase.setOpen_day(sdf.parse((String) map.get(clNames[2])));
                stockBase.setOpen_num(Double.valueOf((String) map.get(clNames[3])));
                stockBase.setCir_num(Double.valueOf((String) map.get(clNames[4])));
                //"cirValue","openType","totalNum","totalRatio","aftCirNum",
                stockBase.setCir_value(Double.valueOf((String) map.get(clNames[5])));
                stockBase.setOpen_type((String) map.get(clNames[6]));
                stockBase.setTotal_num(Double.valueOf((String) map.get(clNames[7])));
                stockBase.setTotal_ratio(Double.valueOf((String) map.get(clNames[8])));
                stockBase.setAft_cir_num(Double.valueOf((String) map.get(clNames[9])));
                //"aftRatio", "befCirNum", "befRatio","openCount","annouDate"
                stockBase.setAft_ratio(Double.valueOf((String) map.get(clNames[10])));
                stockBase.setBef_cir_num(Double.valueOf((String) map.get(clNames[11])));
                stockBase.setBef_ratio(Double.valueOf((String) map.get(clNames[12])));
                stockBase.setOpen_count(Integer.valueOf((String) map.get(clNames[13])));
                stockBase.setAnnou_date(sdf.parse((String) map.get(clNames[14])));
                stockBases.add(stockBase);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //读取完毕,插入数据库
        SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
        StockBaseMapper mapper = sqlSesion.getMapper(StockBaseMapper.class);
        mapper.insertBatch(stockBases);
        System.out.println("插入数据完毕");

    }
    /**
     * @param filePath 文件路径
     * @param sheetNum 表单索引
     * @param fistRow  第一列(列号非索引)
     * @param lastRow  最后一列(列号非索引)
     * @throws Exception
     */
    public void ReadBaseStock2(String filePath, int sheetNum, int fistRow, int lastRow) throws Exception{
        File file = new File(filePath);//"D:\\文件\\限售股解禁公司明细.xls"
        //股票代码,股票名,解禁类型,解禁日
        String[] clNames = new String[]{
                "sCode", "sName", "openType","openDay"
        };
        Map<String, Map<String, Object>> mapMap = PoiUtil.readSheet(file, sheetNum, fistRow, lastRow, clNames);
        List<StockExport> stockExportList = new LinkedList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = fistRow - 1; i < lastRow; i++) {
            try {
                Map<String, Object> map = mapMap.get(String.valueOf(i));
                StockExport stockExport = new StockExport();
                //  "sCode", "sName", "openType","openDay"
                stockExport.setS_code((String) map.get(clNames[0]));
                stockExport.setS_name((String) map.get(clNames[1]));
                stockExport.setOpen_type((String) map.get(clNames[2]));
                stockExport.setOpen_day(sdf.parse((String) map.get(clNames[3])));
                //默认为生效
                stockExport.setStatus("是");
                stockExportList.add(stockExport);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //读取完毕,插入数据库
        SqlSession sqlSesion = SqlSessionUtil.getSqlSesion();
        StockExportMapper mapper = sqlSesion.getMapper(StockExportMapper.class);
        mapper.insertBatch(stockExportList);
        System.out.println("插入数据完毕");

    }

}
